# _*_ encoding:utf-8 _*_
# @Time : 2021/6/20 13:50
# @Author : gx
# _*_ encoding:utf-8 _*_

"""
日志帮助文件
# 根据日志文件的大小保存文件

"""

import logging
import os
import re
import sys
from logging.handlers import TimedRotatingFileHandler,RotatingFileHandler

# TimedRotatingFileHandler 按照日期保存
# RotatingFileHandler 按照文件大小保存


# path = sys.modules['logs']
# path = os.path.dirname(os.path.realpath(path.__file__)) + '/'
path = '../logs/'
if not os.path.exists(path):
    os.mkdir(path)
if not os.path.exists(path+'__init__.py'):
    open(path+'__init__.py', 'w')

class GetLogger(object):
    """
    Set the logs style
    """

    def __init__(self, path, Clevel=logging.DEBUG, Flevel=logging.DEBUG, Elevel=logging.ERROR, when='MIDNIGHT',
                 backupcount=5):
        """
        每天生成日志文件，保留5个日志文件
        :param path: 日志存储路径
        :param Clevel: CMD日志级别
        :param Flevel: 文件日志级别
        :param Elevel: 错误日志级别
        """
        self.logger = logging.getLogger(path)
        self.logger.setLevel(logging.DEBUG)
        fmt = logging.Formatter('[%(levelname)s] [%(asctime)s] %(message)s', '%Y-%m-%d %H:%M:%S')
        # set CMD logs
        shell_log = logging.StreamHandler()
        shell_log.setFormatter(fmt)
        shell_log.setLevel(Clevel)
        # set file logs
        file_log = TimedRotatingFileHandler(filename=path + "day_", when=when, backupCount=backupcount, encoding='utf-8')
        # file_log.suffix = "%Y-%m-%d %H-%M.logs"
        # file_log.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2} \d{2}-\d{2}.logs$")
        file_log.setFormatter(fmt)
        file_log.setLevel(Flevel)
        # set error logs
        error_log = logging.FileHandler(path + 'error.logs', mode='a', encoding='utf-8')
        error_log.setFormatter(fmt)
        error_log.setLevel(Elevel)
        self.logger.addHandler(shell_log)
        self.logger.addHandler(file_log)
        self.logger.addHandler(error_log)

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def error(self, message):
        self.logger.error(message)

    def warning(self, message):
        self.logger.warning(message)

    def critical(self, message):
        self.logger.critical(message)
# logger = GetLogger(path, Clevel=logging.ERROR, Flevel=logging.DEBUG, Elevel=logging.ERROR)

# 按照日志文件的大小保存数据
class GetLoggerRota(object):
    """
    Set the logs style
    """
    def __init__(self, path, Clevel=logging.DEBUG, Flevel=logging.DEBUG, Elevel=logging.ERROR):
        """
        :param path: 日志存储路径
        :param Clevel: CMD日志级别
        :param Flevel: 文件日志级别
        :param Elevel: 错误日志级别
        """
        self.logger = logging.getLogger(path)
        self.logger.setLevel(logging.DEBUG)
        fmt = logging.Formatter('[%(levelname)s] [%(asctime)s] %(message)s', '%Y-%m-%d %H:%M:%S')
        # set CMD logs
        shell_log = logging.StreamHandler()
        shell_log.setFormatter(fmt)
        shell_log.setLevel(Clevel)
        # set file logs
        # file_log = TimedRotatingFileHandler(filename=path + "Day_", when='MIDNIGHT', interval=1, backupCount=5,
        #                                     encoding='utf-8')
        # 文件大小为1G
        file_log = RotatingFileHandler(filename=path + "/Day.logs", mode='a+',
                                       encoding='utf-8', maxBytes=1024*1024*1024*8*1, backupCount=100)
        # file_log.suffix = ".logs"
        # file_log.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}.logs$")
        file_log.setFormatter(fmt)
        file_log.setLevel(Flevel)
        # set error logs
        error_log = logging.FileHandler(path + 'error.logs', mode='a', encoding='utf-8')
        error_log.setFormatter(fmt)
        error_log.setLevel(Elevel)

        self.logger.addHandler(shell_log)
        self.logger.addHandler(file_log)
        self.logger.addHandler(error_log)

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def error(self, message):
        self.logger.error(message)

    def warning(self, message):
        self.logger.warning(message)

    def critical(self, message):
        self.logger.critical(message)



# 日志文件的初始化
# from logger_helper import GetLogger
# 缓存日志cacheDatas
logger = GetLogger(path, Clevel=logging.DEBUG, Flevel=logging.DEBUG,
                      Elevel=logging.ERROR, when='H', backupcount=10)
if __name__ == '__main__':
    logger.debug('test logging')

